package edu.towson.forensics.securephone;

public class SecurePhoneMain {
	
	private static final int TIMING_FOR_ENTIRE_PROCESS=5 *1000; //TODO: Timeout is set to 5 second
	
	private static String adbPath="./res/adb_linux";
	
	private static long timing=0;
	
	public static void main(String[] args) throws Exception 
	{
		timing=System.currentTimeMillis();
		if (System.getProperty("os.name").startsWith("Linux"))
			adbPath="./res/adb_linux";
		if (System.getProperty("os.name").startsWith("Windows"))
			adbPath="./res/adb_windows.exe";
		mainApplication(adbPath);
	}
	
	public static void mainApplication(String adbFilePath) throws Exception
	{		
		String adbCmd=null,securePhoneID = "015D94F406021006";
		int phonePortNumber=0,pcPortNumber=0;
		
		//main value for random port numbers
		int portSeedValue=16000;
		
		/** generate PC in the range of 22000 to 22999 and calculate
		 *  Phone port number based on PC port number.
		 */
		pcPortNumber=portSeedValue+ (int) (Math.random() * 999);
		phonePortNumber = (pcPortNumber<portSeedValue+900) ? pcPortNumber+87 : pcPortNumber-87;
		
		//temporary fixed phone port number to 22221
		phonePortNumber=22221;
	
	/*
	try {	
	Thread.sleep(0 * 1000);
	} catch (InterruptedException e) {
		
	} catch (Exception e){
		
	}
	*/
		
	Runnable timerRunnable=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try{
			Thread.sleep(TIMING_FOR_ENTIRE_PROCESS);
			ProvidedFunctions.UnSecurePhone();
			System.exit(0);
			
			} catch (InterruptedException e){
				
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
	};
	
	Thread timer=new Thread(timerRunnable);
	timer.start();	

	ADBConnection adb=new ADBConnection(pcPortNumber,phonePortNumber,securePhoneID);  //(PC_PORT_NUMBER,PHONE_PORT_NUMBER);

	
/*
    if (adb.isConnedcted())
    	adb.disconnect();
    else   
 */
	
	// assume ADB server is exist
    if (!adb.connectToAdb(adbFilePath))
    {
    	System.out.println("Cannot connect to ADB Server");
    	System.exit(0);
    }
    
	try{
	Thread.sleep(500);
	} catch (InterruptedException e) {
		System.out.println(e.getMessage());
	}
    
    if (adb.chkDeviceId()) 
	{		
//		System.out.println("Connected" + pcPortNumber +" "+ phonePortNumber+ " "+ ProvidedFunctions.pcMacAddress());		
	
	//** send PC name and port number to establish connection
	adbCmd="am startservice -a edu.towson.forensics.securephone.gateway.action " +
			"-c edu.towson.forensics.securephone.category " +
			"-e hostIpOrName PCsendingData -e hostPortNo "+ String.valueOf(phonePortNumber);
	
//	try{
//	Thread.sleep(9000);
//	} catch (InterruptedException e) {
//		System.out.println(e.getMessage());
//	}
	
		adb.runADBCommand(adbCmd);
		
//		adb.forwardport();
		
//		try{
//		Thread.sleep(9000);
//		} catch (InterruptedException e) {
//			System.out.println(e.getMessage());
//		}
		
	    if (!adb.connectToPhone())
	    {
	    	System.out.println("Cannot connect to Phone");
	    	System.exit(0);
	    }
		
		//Send request and check Phone MAC address by predefined MAC address
		if (ProvidedFunctions.chkKeyLock(Key.phoneMacAddress, adb.phoneMacAddress()))
		{
			System.out.println("Correct PhoneMacAddress");
			timer=null;
		}
		else
			ProvidedFunctions.UnSecurePhone();
		
		//Send PC MAC address to Phone
		adb.sendDataToPhone(PhoneRequestedCommands.pcMac);		
		
		//Secure Phone		
		System.out.println("Phone is SecurePhone");
		
		adb.disconnectFromPhone();
		
		System.out.println("Disconnected");
	}
    
	System.out.println(System.currentTimeMillis()-timing);
	System.exit(0);	
	}
}
